<template>
    <div class="search-div">
        <!-- 搜索表单 -->
        <el-form label-width="70px" size="small">
            <el-form-item label="角色名称">
                <el-input style="width: 100%" placeholder="角色名称"></el-input>
            </el-form-item>
            <el-row style="display:flex">
                <el-button type="primary" @click="fetchData" size="small">
                    搜索
                </el-button>
                <el-button size="small">重置</el-button>
            </el-row>
        </el-form>

        <!-- 添加按钮 -->
        <div class="tools-div">
            <el-button type="success" size="small" @click="addModelRef.show()">添 加</el-button>
        </div>

        <!--- 角色表格数据 -->
        <el-table :data="list" style="width: 100%">
            <el-table-column prop="roleName" label="角色名称" width="180" />
            <el-table-column prop="roleCode" label="角色code" width="180" />
            <el-table-column prop="createTime" label="创建时间" />
            <el-table-column label="操作" align="center" width="280">
                <template #default="{ row }">
                    <el-button type="primary" size="small" @click="assignRef.show(row)">分配菜单</el-button>
                    <el-button type="primary" size="small" @click="editModelRef.show(row)">
                        修改
                    </el-button>
                    <el-button type="danger" size="small" @click="handleDelete(row.id)">
                        删除
                    </el-button>
                </template>

            </el-table-column>
        </el-table>

        <!--分页条-->
        <!--分页条-->
        <el-pagination v-model:current-page="pageParams.pageNum" v-model:page-size="pageParams.pageSize"
            :page-sizes="[10, 20, 50, 100]" @size-change="fetchData" @current-change="fetchData"
            layout="total, sizes, prev, pager, next" :total="total" />
    </div>
    <add-model ref="addModelRef" @refresh="fetchData" />
    <edit-model ref="editModelRef" @refresh="fetchData" />
    <Assign ref="assignRef" />
</template>

<script setup>
import { ref } from 'vue';
import addModel from './components/add-model.vue';
import editModel from './components/edit-model.vue';
import { getPageRolesApi,deleteRole } from '@/api/role';
import Assign from './components/assign.vue';
const addModelRef = ref()
const queryDto = ref()
const editModelRef = ref()
const assignRef = ref()
// 分页条总记录数
let total = ref(0)
const pageParams = ref({
    pageNum: 1,
    pageSize: 10
})
// 定义表格数据模型
let list = ref([
    { "id": 9, "roleName": "系统管理员", "roleCode": "xtgly", "createTime": '2023-07-31' },
    { "id": 10, "roleName": "商品管理员", "roleCode": "spgly", "createTime": '2023-07-31' }
])

// 搜索按钮点击事件处理函数
const searchSysRole = () => {
    //queryDto.value.roleName = ""
    fetchData();
}
// 远程调用后端分页查询接口
const fetchData = async () => {

    const { data, code, message } = await getPageRolesApi({
        pageNum: pageParams.value.pageNum,
        pageSize: pageParams.value.pageSize,
        ...queryDto.value

    });
    list.value = data.list;
    total.value = data.total
}

const handleDelete = (id) => {
    ElMessageBox.confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
    }).then(() => {
        // 确定
        deleteRole(id).then(() => {
            ElMessage({
                type: 'success',
                message: '删除成功!'
            })
            fetchData()
        })
    })
}

fetchData()

</script>

<style scoped>
.search-div {
    margin-bottom: 10px;
    padding: 10px;
    border: 1px solid #ebeef5;
    border-radius: 3px;
    background-color: #fff;
}

.tools-div {
    margin: 10px 0;
    padding: 10px;
    border: 1px solid #ebeef5;
    border-radius: 3px;
    background-color: #fff;
}
</style>